
function _dateFormat(date){
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()

    let monthStr = _formatNum(month);
    let dayStr = _formatNum(day);

    return year + '-' + monthStr + '-' + dayStr;
}

function _dateTimeFormat(date){
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    let monthStr = _formatNum(month);
    let dayStr = _formatNum(day);
    let hourStr = _formatNum(hour);
    let minuteStr = _formatNum(minute);
    let secondStr = _formatNum(second);

    return year + '-' + monthStr + '-' + dayStr + ' ' + hourStr + ':' + minuteStr + ":" + secondStr;
}

function _timeFormat(time) {
    let times = parseInt(time/1000); //秒
    let h = parseInt(times/(60*60));
    times = times%(60*60);
    const m = u_checkTime(parseInt(times/60));
    const s = u_checkTime(parseInt(times%60));
    h =h>0?((h>9?h:'0'+h)+":"):'';

    return `${h}${m}:${s}`;
}

function _dateTiemParse(dateStr){
    let timestamp = Date.parse(dateStr);
    return new Date(timestamp);
}

function _addDay(date,num){
    let result = new Date(date.getTime());
    // 将时间增加一天
    result.setDate(date.getDate() + num);
    return result;
}

function _diffDay(startDate,endDate){
    let times = 24*3600*1000;       //1天的毫秒数
    let endTimes = Math.ceil(endDate.getTime() / times);
    let startTimes = Math.ceil(startDate.getTime() / times);
    //console.log(endTimes + "," + startTimes);
    let day = endTimes - startTimes;
    return day;
}

function _formatNum(n){
    n = n.toString()
    return n[1] ? n : '0' + n
}

//是否是数字
function _isNum(val){
  // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除，
　if(val === "" || val ==null){
    return false;
　}
  if(!isNaN(val)){　　　　
　　 //对于空数组和只有一个数值成员的数组或全是数字组成的字符串，isNaN返回false，例如：'123'、[]、[2]、['123'],isNaN返回false,
    //所以如果不需要val包含这些特殊情况，则这个判断改写为if(!isNaN(val) && typeof val === 'number' )
　  return true; 
　}else{ 
　  return false; 
　} 
}

//是否是方法
function _isFunction( obj ) {
  return typeof obj === "function";
}

//是否是数组
function _isArray( obj ) {
  return typeof obj === "array";
}

//是否是对象
function _isObject(arg){
    return Object.prototype.toString.call(arg) === '[object Object]'
}

//是否是手机号
function _isPhone(arg) {
    return /^1[3456789]\d{9}$/.test(arg)
}

//16进制字符转bufferArray
function _toBufferAry(hex) { 
    return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
      return parseInt(h, 16)
    })).buffer
}

//16进制转字符
function _ab2hex(buffer) {
    if(buffer){
        var hexArr = Array.prototype.map.call(
            new Uint8Array(buffer),
            function (bit) {
            return ('00' + bit.toString(16)).slice(-2)
            }
        )
        return hexArr.join('');
    }
    return '';
}

//url上拼接参数
function _getUrl(url,obj){
    if(!this._isObject(obj))return url;
    for(let key in obj){
        url+=url.indexOf('?')==-1?'?':'&'
        url+=encodeURIComponent(key)+'='+encodeURIComponent(obj[key]);
    }
    return url;
}

//图片转base64
function _toBase64OFImg(url){
    return new Promise((resolve)=>{
        my.getFileSystemManager().readFile({
            filePath:url,
            encoding:'base64',
            success(res){
                resolve('data:image/jpeg;base64,'+res.data);
            },
            fail(err){
                console.log(err.errMsg)
            }
        })
    })
}

module.exports = {
  dateFormat:_dateFormat,
  dateTimeFormat:_dateTimeFormat,
  dateTiemParse:_dateTiemParse,
  timeFormat:_timeFormat,
  addDay:_addDay,
  diffDay:_diffDay,

  formatNum:_formatNum,

  isNum:_isNum,
  isFunction:_isFunction,
  isArray:_isArray,
  isObject:_isObject,
  isPhone:_isPhone,

  toBufferAry: _toBufferAry,
  ab2hex: _ab2hex,
  toBase64OFImg:_toBase64OFImg,

  getUrl:_getUrl
}
